package com.xls.dormitoryservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.dormitoryservice.entity.Notice;
import com.xls.dormitoryservice.service.NoticeService;
import com.xls.dormitoryservice.vo.NoticeQuery;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/dormitory/notice")
@Api(value = "公告管理")
@Slf4j
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @Log(title = "公告管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:notice:list','dormitory:noticeInfo:list')")
    @GetMapping("/getNotice/{id}")
    public Result<Notice> getNotice(@PathVariable String id) {
        return Result.success(noticeService.getById(id));
    }

    @Log(title = "公告管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:notice:list','dormitory:noticeInfo:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<Notice>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                     @RequestBody NoticeQuery noticeQuery) {
        return Result.success(noticeService.getNoticePage(page, limit, noticeQuery));
    }

    @Log(title = "公告管理", businessType = "添加")
    @PreAuthorize("hasAnyAuthority('dormitory:notice:save')")
    @PostMapping("/save")
    public Result<Notice> save(@RequestBody Notice notice) {
        return noticeService.saveNotice(notice) ? Result.success(notice) : Result.error();
    }

    @Log(title = "公告管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('dormitory:notice:update')")
    @PostMapping("/update")
    public Result<Notice> update(@RequestBody Notice notice) {
        return noticeService.updateNotice(notice) ? Result.success(notice) : Result.error();
    }

    @Log(title = "公告管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('dormitory:notice:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<Notice> remove(@PathVariable String id) {
        return noticeService.removeById(id) ? Result.success() : Result.error();
    }
}

